---
title: "<center> The effect of terrorist attacks on attitudes and its duration</center> <center>Replication</center>"
author: "<center> Oguzhan Turkoglu and Thomas Chadefaux</center>"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r}
library(knitr)
library(RColorBrewer)
library(gplots)
library(lubridate)
library(lme4)
library(multiwayvcov)
library(sandwich)
library(lmtest)
library(xtable)
library(stargazer)
library(readxl)
library(sjPlot)
library(tidyverse)
```


```{r fig.height = 20, fig.width = 12, fig.align = "center"}
#############FIGURE 1##############
#import the dataset
load("main.RData")

#select the dependent variables
dv <- c( "let_immigrants_different", 
         "satisfaction_life", "satisfaction_government", 
         "satisfaction_economy", "trust_parliament", 
         "trust_legal_system", "trust_police", 
         "immigration_improves_country")

#loop for the regression results
coefs <- ses <- variables <- NULL
days <- c(1:15, 20, 30)
for(i in 1:length(dv)){
  for(k in 1:17){
    day.formula <- as.formula(paste(paste(dv[i]), paste("~ success_", days[k], sep = ""), paste("+ post_", days[k], sep =""),  paste("+ success_unit_", days[k], sep = ""),  paste("+ attack_number_", days[k], sep = ""), 
                                    "+ attack_last_year + as.factor(country) + as.factor(year)", collapse=''))
    
    model  <- lm(day.formula, data = ess, weights = post_stratification_weights)
    vcov_country <- cluster.vcov(model, ess$country)
    coefs <- c(coefs, model$coefficients[paste("success_", days[k], sep = "")])
    ses <- c(ses, coeftest(model, vcov_country)[2,2])
    variables <- c(variables, dv[i])
    
    rm(day.formula, model, vcov_country)
  }
  print(i)
}


#create the dataset to plot
toplot <- data.frame(coefs = coefs, ses = ses, variables = variables, day = 1:17)
toplot$upper <- toplot$coefs + 1.96*(toplot$ses)
toplot$lower <- toplot$coefs - 1.96*(toplot$ses)

# redefine the order in which the variables appear
toplot$variables <- factor(toplot$variables, levels = c("satisfaction_life",
                                                        'satisfaction_government',
                                                        "trust_parliament", 
                                                        'trust_legal_system',
                                                        'trust_police',
                                                        "satisfaction_economy",
                                                        'let_immigrants_different',
                                                        'immigration_improves_country'
))

# change the way the variable names appear
levels(toplot$variables)[levels(toplot$variables)=="satisfaction_life"] <- "Life satisfaction"
levels(toplot$variables)[levels(toplot$variables)=="satisfaction_government"] <- "Government satisfaction"
levels(toplot$variables)[levels(toplot$variables)=="immigration_improves_country"] <- "Immigration is beneficial"
levels(toplot$variables)[levels(toplot$variables)=="trust_parliament"] <- "Trust parliament"
levels(toplot$variables)[levels(toplot$variables)=="trust_legal_system"] <- "Trust legal system"
levels(toplot$variables)[levels(toplot$variables)=="trust_police"] <- "Trust police"
levels(toplot$variables)[levels(toplot$variables)=="satisfaction_economy"] <- "Satisfaction with economy"
levels(toplot$variables)[levels(toplot$variables)=="let_immigrants_different"] <- "Accept immigrants\n from different race"

ggplot(toplot, aes(day, coefs)) + 
  geom_segment(aes(x=1, y=0, xend=17, yend=0, color='red'), linetype="dashed", lwd=1.1)+ ## Not sure why the dotted is not working
  theme_classic() +
  geom_line(colour='darkgrey', lwd=1.2)+
  geom_point(aes(size=1.2),colour = 'black') + 
  theme(legend.position="none", plot.margin = margin(0.5, 0.6, 0.5, 0, "cm"), 
        axis.text.y =  element_text(size = 16), axis.text.x = element_text(size = 16), 
        axis.title.y =  element_text(size = 24), axis.title.x = element_text(size = 24), 
        plot.title = element_text(hjust = 0.5, size = 22), 
        strip.text.x = element_text(size = 20),
        strip.background = element_rect(fill='grey', color='white')) +
  labs(x = "Number of Days After Attack", y = "Effect", title = "") +
  scale_x_continuous(breaks = 1:17, labels = c(1:15, 20, 30)) +
  facet_wrap(~variables, ncol = 2, scale='free')+
  geom_ribbon(aes(ymin=lower, ymax=upper), fill="grey", alpha=0.2)+
  coord_cartesian(ylim = c(-0.3, 0.2)) 


rm(list = setdiff(ls(), "ess"))

```


```{r}
#############TABLE A1############
#summary statistics
summ.stats <- select(ess, male, age, married, high_school, university, vocational, employed, 
                     standardized_hh, christian, jewish, muslim, discriminated_group_member, 
                     father_high_school, father_university, father_vocational, mother_high_school, 
                     mother_university, mother_vocational, satisfaction_life, satisfaction_government,
                     attack, successful_attack)
stargazer(summ.stats, omit.summary.stat = c("p25", "p75"), type = "text")
rm(list = setdiff(ls(), "ess"))
```

```{r message=FALSE, warning=FALSE}
###############TABLE A2###############
gtd <- read_excel("terrorism.xlsx")

#keeping only relevant countries
gtd <- gtd[gtd$country_txt %in% unique(ess$country),]

#gtd date
gtd$date <- str_c(gtd$iyear, gtd$imonth, gtd$iday, sep = "-")
gtd$date <- ymd(gtd$date)

#keep if there is date info
gtd <- gtd[!is.na(gtd$date),]

#common country variable
gtd$country <- NULL
gtd <- rename(gtd, country = country_txt)

#get the unique ess data
ess_base <- select(ess, country, date)
ess_base <- unique(ess_base)

#create the data for the last 30 days
ess_loop <- ess_base
for(i in 1:30){
  ess_to_merge <- ess_loop 
  ess_to_merge$date <- ess_to_merge$date - i
  ess_base <- rbind(ess_base, ess_to_merge)
  rm(ess_to_merge)
}
ess_base <- unique(ess_base)
ess_base$to_keep <- 1

#merge 
gtd <- merge(gtd, ess_base, by = c("country", "date"), all.x = T)
gtd <- gtd[gtd$to_keep == 1 & !is.na(gtd$to_keep), ]

#creating binary descriptives
gtd$armed_assault <- ifelse(gtd$attacktype1_txt == "Armed Assault", 1, 0)
gtd$assasination <- ifelse(gtd$attacktype1_txt == "Assassination", 1, 0)
gtd$bombing <- ifelse(gtd$attacktype1_txt == "Bombing/Explosion", 1, 0)
gtd$facility_infra <- ifelse(gtd$attacktype1_txt == "Facility/Infrastructure Attack", 1, 0)
gtd$attack_other <- ifelse(gtd$attacktype1_txt %in% c("Hijacking", "Hostage Taking (Barricade Incident)", "Hostage Taking (Kidnapping)", "Unarmed Assault", "Unknown"), 1, 0)
gtd$explosives <- ifelse(gtd$weaptype1_txt == "Explosives", 1, 0)
gtd$firearms <- ifelse(gtd$weaptype1_txt == "Firearms", 1, 0)
gtd$incendiary <- ifelse(gtd$weaptype1_txt == "Incendiary", 1, 0)
gtd$weapon_other <- ifelse(gtd$weaptype1_txt %in% c("Explosives", "Firearms", "Incendiary"), 0 , 1)
gtd$international_attack <- ifelse(gtd$INT_ANY == 1, 1, 0)
gtd$civilian_target <- ifelse(gtd$targtype1_txt %in% c("Military", "Police", "Other", "Unknown", "Terrorists/Non-State Militia"), 0, 1)

#get the necessary variables 
sum_data <- select(gtd, success, nkill, armed_assault, assasination, facility_infra, attack_other,
                   explosives, firearms, incendiary, weapon_other, international_attack, civilian_target)
stargazer(sum_data,  omit.summary.stat = c("n", "p25", "p75"),  type = "text")
rm(list = setdiff(ls(), "ess"))
```

```{r fig.height = 9, fig.width = 9, fig.align = "center", message=FALSE, warning=FALSE}
###############FIGURE A1###############
gtd <- read_excel("terrorism.xlsx")
gtd$attack <- 1
gtd <- gtd[gtd$country_txt %in% unique(ess$country),]
gtd$date <- str_c(gtd$iyear, gtd$imonth, gtd$iday, sep = "-")
gtd$date <- ymd(gtd$date)
gtd <- gtd[!is.na(gtd$date),]

#keep it compatible with the ESS sample
gtd <- gtd[gtd$iyear > 2001,]
gtd <- gtd[!(gtd$country_txt =='Ukraine' & gtd$iyear >= 2014),]
attacksByYear <- aggregate(gtd$attack, by=list(gtd$iyear), FUN=length)
attacksByYear$x <- 1+(attacksByYear$x)
successesByYear <- aggregate(gtd$success, by=list(gtd$iyear), FUN=sum)
successesByYear$x <- 1+(successesByYear$x)
failuresByYear <- aggregate(gtd$attack[gtd$success==0], by=list(gtd$iyear[gtd$success==0]), FUN=length)
failuresByYear$x <- 1+(failuresByYear$x)
  
par(mar=c(6,8,1,1))
plot((successesByYear), type='l', log='y', ylim=c(5,1500), lwd=2, lty=1, 
       col=1, las=1, cex.axis=2, ylab='', cex.lab=2, xlab='')
  mtext(1, text = 'Year', line=4, cex=3)
  mtext(2, text = 'Number of events', line=5, cex=3)
  lines(failuresByYear, col=1, lwd=2, lty=2)
  legend('topleft', legend = c('Successful attacks', 'Failed attacks'), lwd=2, lty=c(1,2), cex=1.8)

rm(list = setdiff(ls(), "ess"))
```


```{r fig.height = 9, fig.width = 9, fig.align = "center", message=FALSE, warning=FALSE}
###############FIGURE A2###############
gtd <- read_excel("terrorism.xlsx")
gtd$attack <- 1
gtd <- gtd[gtd$country_txt %in% c(unique(ess$country), "Israel", "Russia", "Turkey"),]
gtd$date <- str_c(gtd$iyear, gtd$imonth, gtd$iday, sep = "-")
gtd$date <- ymd(gtd$date)
gtd <- gtd[!is.na(gtd$date),]

#keep it compatible with the ESS sample
gtd <- gtd[gtd$iyear > 2001,]
gtd <- gtd[!(gtd$country_txt =='Ukraine' & gtd$iyear >= 2014),]

breakScale <- c(0, 1, 5, 20, 50, 100, 200, 500)
heatmap.2(log1p(table(factor(gtd$country_txt[gtd$success==1],
                             levels = sort(unique(gtd$country_txt))), gtd$iyear[gtd$success==1])), 
          dendrogram='none', Rowv=FALSE,
          Colv=FALSE,trace='none',
          scale='none',
          
          col=brewer.pal(length(breakScale)-1, 'Greys'), 
          breaks=log1p(breakScale), key=T,
          cexRow=1.5, cexCol=2, density.info="none", 
          key.title=NA, key.xlab = '',
          key.par = list(cex=1.5,mar=c(2,0.5,1.5,0.5)),
          keysize=1,
          lhei =c(8, 2),
          lwid = c(1, 5, 1),
          lmat = rbind(  c(6,1,3),c(5,4,2) ),
          
          key.xtickfun=function() {
            breaks <- parent.frame()$breaks
            return(list(
              at=parent.frame()$scale01(c(breaks[1:length(breaks)-1],
                                          breaks[length(breaks)])),
              labels=breakScale)
            )
          })




breakScale <- c(0,1,5,20,50,100,200,500)
heatmap.2(log1p(table(factor(gtd$country_txt[gtd$success==0], 
                               levels = sort(unique(gtd$country_txt))),
                        gtd$iyear[gtd$success==0])), 
            dendrogram='none', Rowv=FALSE,
            Colv=FALSE,trace='none',
            scale='none',
            
            col=brewer.pal(length(breakScale)-1, 'Greys'), 
            breaks=log1p(breakScale), key=F,
            cexRow=1.5, cexCol=2, density.info="none", 
            key.title=NA, key.xlab = '',
            key.par = list(cex=1.5,mar=c(2,0.5,1.5,0.5)),
            keysize=1,
            lhei =c(8, 2),
            lwid = c(1, 5, 1),
            lmat = rbind(  c(6,1,3),c(5,4,2) ),
            key.xtickfun=function() {
              breaks <- parent.frame()$breaks
              return(list(
                at=parent.frame()$scale01(c(breaks[1:length(breaks)-1],
                                            breaks[length(breaks)])),
                labels=breakScale)
              )
            })
rm(list = setdiff(ls(), "ess"))
```

```{r}
opts <- options(knitr.kable.NA = "")
#########TABLE A3#########
attack <- no_attack <- success <- failure <- attack_diff <- success_diff <- variables <- NULL

#MALE
attack <- c(attack, mean(ess$male[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$male[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$male[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$male[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(male ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(male ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$male[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$male[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$male[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$male[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(male ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(male ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Male", NA))

#AGE
attack <- c(attack, mean(ess$age[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$age[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$age[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$age[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(age ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(age ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$age[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$age[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$age[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$age[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(age ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(age ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Age", NA))

#MARRIED/LIVING TOGETHER
attack <- c(attack, mean(ess$married[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$married[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$married[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$married[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(married ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(married ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$married[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$married[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$married[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$married[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(married ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(married ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Married/Partner", NA))

#HIGH SCHOOL
attack <- c(attack, mean(ess$high_school[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$high_school[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$high_school[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$high_school[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(high_school ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(high_school ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$high_school[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$high_school[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$high_school[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$high_school[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(high_school ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(high_school ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("High School", NA))

#UNIVERSITY
attack <- c(attack, mean(ess$university[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$university[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$university[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$university[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(university ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(university ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$university[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$university[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$university[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$university[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(university ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(university ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("University", NA))

#VOCATIONAL
attack <- c(attack, mean(ess$vocational[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$vocational[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$vocational[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$vocational[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(vocational ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(vocational ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$vocational[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$vocational[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$vocational[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$vocational[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(vocational ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(vocational ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Vocational", NA))

#EMPLOYED
attack <- c(attack, mean(ess$employed[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$employed[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$employed[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$employed[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(employed ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(employed ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$employed[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$employed[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$employed[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$employed[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(employed ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(employed ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Employed", NA))

#HH INCOME
attack <- c(attack, mean(ess$standardized_hh[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$standardized_hh[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$standardized_hh[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$standardized_hh[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(standardized_hh ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(standardized_hh ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$standardized_hh[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$standardized_hh[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$standardized_hh[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$standardized_hh[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(standardized_hh ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(standardized_hh ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("HH Income", NA))

#CHRISTIAN
attack <- c(attack, mean(ess$christian[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$christian[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$christian[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$christian[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(christian ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(christian ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$christian[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$christian[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$christian[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$christian[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(christian ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(christian ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Christian", NA))

#JEWISH
attack <- c(attack, mean(ess$jewish[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$jewish[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$jewish[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$jewish[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(jewish ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(jewish ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$jewish[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$jewish[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$jewish[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$jewish[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(jewish ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(jewish ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Jewish", NA))

#MUSLIM
attack <- c(attack, mean(ess$muslim[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$muslim[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$muslim[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$muslim[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(muslim ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(muslim ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$muslim[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$muslim[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$muslim[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$muslim[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(muslim ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(muslim ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Muslim", NA))

#DISCRIMINATED GROUP MEMBER
attack <- c(attack, mean(ess$discriminated_group_member[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$discriminated_group_member[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$discriminated_group_member[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$discriminated_group_member[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(discriminated_group_member ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(discriminated_group_member ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$discriminated_group_member[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$discriminated_group_member[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$discriminated_group_member[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$discriminated_group_member[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(discriminated_group_member ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(discriminated_group_member ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Discriminated", NA))

#FATHER HIGH SCHOOL
attack <- c(attack, mean(ess$father_high_school[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$father_high_school[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$father_high_school[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$father_high_school[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(father_high_school ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(father_high_school ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$father_high_school[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$father_high_school[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$father_high_school[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$father_high_school[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(father_high_school ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(father_high_school ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Father High School", NA))

#FATHER UNIVERSITY
attack <- c(attack, mean(ess$father_university[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$father_university[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$father_university[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$father_university[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(father_university ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(father_university ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$father_university[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$father_university[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$father_university[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$father_university[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(father_university ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(father_university ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Father University", NA))

#FATHER VOCATIONAL
attack <- c(attack, mean(ess$father_vocational[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$father_vocational[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$father_vocational[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$father_vocational[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(father_vocational ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(father_vocational ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$father_vocational[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$father_vocational[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$father_vocational[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$father_vocational[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(father_vocational ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(father_vocational ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Father Vocational", NA))

#MOTHER HIGH SCHOOL
attack <- c(attack, mean(ess$mother_high_school[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$mother_high_school[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$mother_high_school[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$mother_high_school[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(mother_high_school ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(mother_high_school ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$mother_high_school[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$mother_high_school[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$mother_high_school[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$mother_high_school[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(mother_high_school ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(mother_high_school ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Mother High School", NA))

#MOTHER UNIVERSITY
attack <- c(attack, mean(ess$mother_university[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$mother_university[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$mother_university[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$mother_university[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(mother_university ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(mother_university ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$mother_university[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$mother_university[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$mother_university[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$mother_university[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(mother_university ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(mother_university ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Mother University", NA))

#MOTHER VOCATIONAL
attack <- c(attack, mean(ess$mother_vocational[ess$attack == 1], na.rm = T))
attack <- c(attack, sd(ess$mother_vocational[ess$attack == 1], na.rm = T))
no_attack <- c(no_attack, mean(ess$mother_vocational[ess$attack == 0], na.rm = T))
no_attack <- c(no_attack, sd(ess$mother_vocational[ess$attack == 0], na.rm = T))
attack_diff <- c(attack_diff, lm(mother_vocational ~ attack, ess)$coefficients[["attack"]])
attack_diff <- c(attack_diff, sqrt(diag(vcov(lm(mother_vocational ~ attack, ess))))[["attack"]])
success <- c(success, mean(ess$mother_vocational[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
success <- c(success, sd(ess$mother_vocational[ess$attack == 1 & ess$successful_attack == 1], na.rm = T))
failure <- c(failure, mean(ess$mother_vocational[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
failure <- c(failure, sd(ess$mother_vocational[ess$attack == 1 & ess$successful_attack == 0], na.rm = T))
success_diff <- c(success_diff, lm(mother_vocational ~ successful_attack, ess[ess$attack == 1,])$coefficients[["successful_attack"]])
success_diff <- c(success_diff, sqrt(diag(vcov(lm(mother_vocational ~ successful_attack, ess[ess$attack == 1,]))))[["successful_attack"]])
variables <- c(variables, c("Mother Vocational", NA))

to_present <- data.frame(variables, attack, no_attack, attack_diff, success, failure, success_diff)
to_present$attack_diff_lead <- lead(to_present$attack_diff)
to_present$success_diff_lead <- lead(to_present$success_diff)
to_present$attack_p <- to_present$attack_diff/to_present$attack_diff_lead
to_present$success_p <- to_present$success_diff/to_present$success_diff_lead

#rounding values
to_present$attack <- round(to_present$attack, 3)
to_present$no_attack <- round(to_present$no_attack, 3)
to_present$attack_diff <- round(to_present$attack_diff, 3)
to_present$success <- round(to_present$success, 3)
to_present$failure <- round(to_present$failure, 3)
to_present$success_diff <- round(to_present$success_diff, 3)

#stars
to_present$attack_p <- ifelse(abs(to_present$attack_p) > 2.581, "**", 
                              ifelse(abs(to_present$attack_p) > 1.96, "*", ""))
to_present$success_p <- ifelse(abs(to_present$success_p) > 2.581, "**", 
                               ifelse(abs(to_present$success_p) > 1.96, "*", ""))

to_present$attack_p[seq(2,36,2)] <- ""
to_present$success_p[seq(2,36,2)] <- ""

to_present$attack_diff <- str_c(to_present$attack_diff, to_present$attack_p, sep ="")
to_present$success_diff <- str_c(to_present$success_diff, to_present$success_p, sep ="")

#parentheses for SEs
to_present$attack[seq(2,36, 2)] <- str_c("(", to_present$attack[seq(2,36, 2)], ")")
to_present$no_attack[seq(2,36, 2)] <- str_c("(", to_present$no_attack[seq(2,36, 2)], ")")
to_present$attack_diff[seq(2,36, 2)] <- str_c("(", to_present$attack_diff[seq(2,36, 2)], ")")
to_present$success[seq(2,36, 2)] <- str_c("(", to_present$success[seq(2,36, 2)], ")")
to_present$failure[seq(2,36, 2)] <- str_c("(", to_present$failure[seq(2,36, 2)], ")")
to_present$success_diff[seq(2,36, 2)] <- str_c("(", to_present$success_diff[seq(2,36, 2)], ")")

#printing
to_present <- to_present[, 1:7]
colnames(to_present) <- c("Variable", "Attack", "No Attack", "Difference", "Success", "Failure", "Difference")
kable(to_present, row.names = FALSE)
```



```{r message=FALSE, warning=FALSE}
##########TABLE A4########
#importing the terror dataset
gtd <- read_excel("terrorism.xlsx")

#attacks after 2001
gtd <- gtd[gtd$iyear > 2001, ]

#keeping only relevant countries
gtd <- gtd[gtd$country_txt %in% unique(ess$country),]

#gtd date
gtd$date <- ifelse(gtd$imonth < 10, str_c(0, gtd$imonth, sep = ""), gtd$imonth)
gtd$date <- str_c(gtd$iyear, gtd$date, sep = "-")

#collapse gtd
gtd <- gtd %>%
  group_by(country_txt, date) %>%
  summarise(n_attack = n(), n_deaths = sum(nkill, na.rm = T), success = max(success))
gtd <- rename(gtd, country = country_txt)

#creating main data
countries <- data.frame(country = unique(ess$country))
countries <- countries %>%
  rowwise() %>%
  do(data.frame(country = .$country, year = seq(2002, 2017)))
countries <- countries %>%
  rowwise() %>%
  do(data.frame(country = .$country, year = .$year, month = seq(1, 12)))
countries$month <- ifelse(countries$month < 10, str_c(0, countries$month), countries$month)
countries$date <- str_c(countries$year, countries$month, sep = "-")

#dropping Ukraine 2014 and onwards as the civil conflict started then
countries$todrop <- ifelse(countries$country == "Ukraine" & countries$year > 2013, 1, 0)
countries <- countries[countries$todrop == 0, ]
countries$todrop <- NULL

#merging two datasets
countries <- merge(countries, gtd, by = c("country", "date"), all.x = T)
countries$n_attack <- ifelse(is.na(countries$n_attack), 0, countries$n_attack)
countries$n_deaths <- ifelse(is.na(countries$n_deaths), 0, countries$n_deaths)
countries$attack <- ifelse(countries$n_attack > 0, 1, 0)
countries$month <- as.factor(countries$month)

#models
model.attack <- glm(attack ~ as.factor(month) + as.factor(country) + as.factor(year), data = countries, family = "binomial")
vcov_country <- cluster.vcov(model.attack, countries$country)
coefs_attack <- coeftest(model.attack, vcov_country)

model.success <- glm(success ~ as.factor(month) + as.factor(country) + as.factor(year), data = countries, family = "binomial")
vcov_country_suc <- cluster.vcov(model.success, countries$country)
coefs_success <- coeftest(model.success, vcov_country_suc)

topresent_attack <- data.frame(coef.attack = coefs_attack[,1], ses.attack = coefs_attack[,2], p.attack = coefs_attack[,4])
topresent_attack <- topresent_attack[-13:-44, ]
topresent_attack$ses.attack <- round(topresent_attack$ses.attack, 3)
topresent_attack$coef.attack <- round(topresent_attack$coef.attack, 3)
topresent_attack$coef.attack <- ifelse(topresent_attack$p.attack < .01, str_c(topresent_attack$coef.attack, "**"),
                                       ifelse(topresent_attack$p.attack < 0.05, str_c(topresent_attack$coef.attack, "*"), topresent_attack$coef.attack))
topresent_success <- data.frame(coef.success = coefs_success[,1], ses.success = coefs_success[,2], p.success = coefs_success[,4])
topresent_success <- topresent_success[-13:-41, ]
topresent_success$ses.success <- round(topresent_success$ses.success, 3)
topresent_success$coef.success <- round(topresent_success$coef.success, 3)
topresent_success$coef.success <- ifelse(topresent_success$p.success < .01, str_c(topresent_success$coef.success, "**"),
                                       ifelse(topresent_success$p.success < 0.05, str_c(topresent_success$coef.success, "*"), topresent_success$coef.success))

topresent <- cbind(topresent_attack[, 1:2], topresent_success[, 1:2])
topresent <- rbind(topresent, c(rep(nrow(model.attack$model), 2), rep(nrow(model.success$model), 2)))
topresent$ses.attack <- as.character(topresent$ses.attack)
topresent$ses.success <- as.character(topresent$ses.success)
topresent$variable <- rownames(topresent)
topresent$variable <- str_remove(topresent$variable, "as.factor\\(year\\)")
topresent$variable <- str_remove(topresent$variable, "as.factor\\(month\\)")
topresent$variable[topresent$variable == "02"] <- "February"
topresent$variable[topresent$variable == "03"] <- "March"
topresent$variable[topresent$variable == "04"] <- "April"
topresent$variable[topresent$variable == "05"] <- "May"
topresent$variable[topresent$variable == "06"] <- "June"
topresent$variable[topresent$variable == "07"] <- "July"
topresent$variable[topresent$variable == "08"] <- "August"
topresent$variable[topresent$variable == "09"] <- "September"
topresent$variable[topresent$variable == "10"] <- "October"
topresent$variable[topresent$variable == "11"] <- "November"
topresent$variable[topresent$variable == "12"] <- "December"
topresent <- topresent[, c(5, 1:4)]
colnames(topresent) <- c("Variable", "Attack Coef", "Attack SE", "Success Attack", "Succcess SE")
rownames(topresent) <- 1:nrow(topresent)
topresent$Variable[nrow(topresent)] <- "N"
kable(topresent, row.names = FALSE)
rm(list = setdiff(ls(), "ess"))
```

```{r}

###########TABLE A5####
#1 DAY
#LIFE SATISFACTION
#N Attack, Attack Last Year
model_type <- "N Attack, N Attack Last Year"
coefs_1 <- ses_1 <- p_values_1 <- n_1 <- NULL
model  <- lm(satisfaction_life ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model_type <- c(model_type, "Weapon and Attack FE, N Death")
model  <- lm(satisfaction_life ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model_type <- c(model_type, "Int Terror Group")
model  <- lm(satisfaction_life ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               international_attack_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model_type <- c(model_type, "Civilian Target")
model  <- lm(satisfaction_life ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               civilian_target_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#All FE
model_type <- c(model_type, "All")
model  <- lm(satisfaction_life ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               international_attack_1 + civilian_target_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model_type <- c(model_type, "N Attack, N Attack Last Year")
model  <- lm(satisfaction_government ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model_type <- c(model_type, "Weapon and Attack FE, N Death")
model  <- lm(satisfaction_government ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model_type <- c(model_type, "Int Terror Group")
model  <- lm(satisfaction_government ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               international_attack_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model_type <- c(model_type, "Civilian Target")
model  <- lm(satisfaction_government ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               civilian_target_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#All FE
model_type <- c(model_type, "All")
model  <- lm(satisfaction_government ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               international_attack_1 + civilian_target_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#5 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_5 <- ses_5 <- p_values_5 <- n_5 <- NULL
model  <- lm(satisfaction_life ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_life ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_life ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               international_attack_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_life ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               civilian_target_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_life ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               international_attack_5 + civilian_target_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(satisfaction_government ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_government ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_government ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               international_attack_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_government ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               civilian_target_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_government ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               international_attack_5 + civilian_target_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)


#10 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_10 <- ses_10 <- p_values_10 <- n_10 <- NULL
model  <- lm(satisfaction_life ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_life ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_life ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               international_attack_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_life ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               civilian_target_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_life ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               international_attack_10 + civilian_target_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(satisfaction_government ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_government ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_government ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               international_attack_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_government ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               civilian_target_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_government ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               international_attack_10 + civilian_target_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)


#20 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_20 <- ses_20 <- p_values_20 <- n_20 <- NULL
model  <- lm(satisfaction_life ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_life ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_life ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               international_attack_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_life ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               civilian_target_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_life ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               international_attack_20 + civilian_target_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(satisfaction_government ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_government ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_government ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               international_attack_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_government ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               civilian_target_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_government ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               international_attack_20 + civilian_target_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)


#30 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_30 <- ses_30 <- p_values_30 <- n_30 <- NULL
model  <- lm(satisfaction_life ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_life ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_life ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               international_attack_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_life ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               civilian_target_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_life ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               international_attack_30 + civilian_target_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(satisfaction_government ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_government ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_government ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               international_attack_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_government ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               civilian_target_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_government ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               international_attack_30 + civilian_target_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#TABLE TO PRESENT
p_values_1 <- ifelse(p_values_1 < 0.05 & p_values_1 > 0.01, "*",
                     ifelse(p_values_1 < 0.01, "**", ""))
p_values_5 <- ifelse(p_values_5 < 0.05 & p_values_5 > 0.01, "*",
                     ifelse(p_values_5 < 0.01, "**", ""))
p_values_10 <- ifelse(p_values_10 < 0.05 & p_values_10 > 0.01, "*",
                      ifelse(p_values_10 < 0.01, "**", ""))
p_values_20 <- ifelse(p_values_20 < 0.05 & p_values_20 > 0.01, "*",
                      ifelse(p_values_20 < 0.01, "**", ""))
p_values_30 <- ifelse(p_values_30 < 0.05 & p_values_30 > 0.01, "*",
                      ifelse(p_values_30 < 0.01, "**", ""))
coefs_1 <- str_c(round(coefs_1, 3), p_values_1)
coefs_5 <- str_c(round(coefs_5, 3), p_values_5)
coefs_10 <- str_c(round(coefs_10, 3), p_values_10)
coefs_20 <- str_c(round(coefs_20, 3), p_values_20)
coefs_30 <- str_c(round(coefs_30, 3), p_values_30)
ses_1 <- str_c("(", round(ses_1, 3), ")")
ses_5 <- str_c("(", round(ses_5, 3), ")")
ses_10 <- str_c("(", round(ses_10, 3), ")")
ses_20 <- str_c("(", round(ses_20, 3), ")")
ses_30 <- str_c("(", round(ses_30, 3), ")")
line_1 <- rep("Yes", 10)
line_2 <- rep("Yes", 10)
line_3 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_4 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_5 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_6 <- c(rep("No", 2), "Yes", "No", "Yes", rep("No", 2), "Yes", "No", "Yes")
line_7 <- c(rep("No", 3), rep("Yes", 2), rep("No", 3), rep("Yes", 2))

to_present <- rbind(coefs_1, ses_1, n_1, coefs_5, ses_5, n_5, coefs_10, ses_10, n_10, 
                    coefs_20, ses_20, n_20, coefs_30, ses_30, n_30, line_1, line_2,
                    line_3, line_4, line_5, line_6, line_7)

c_to_bind <- c("Success x Post (1 Day)", NA, "N", "Success x Post (5 Day)", NA, "N", 
               "Success x Post (10 Day)", NA, "N", "Success x Post (20 Day)", NA, "N", 
               "Success x Post (30 Day)", NA, "N", "N Attack", "N Attack Year", 
               "N Deaths", "Attack Type FE", "Weapon Type FE", "International Group", "Civilian Target")
to_present <- as.data.frame(cbind(c_to_bind, to_present))
colnames(to_present) <- c("", "(1)", "(2)", "(3)", "(4)", "(5)", "(6)", "(7)", "(8)", "(9)", "(10)")
kable(to_present,  row.names = FALSE)
```


```{r}
#################TABLE A6###############
rm(list = setdiff(ls(), "ess"))
#1 DAY
#LIFE SATISFACTION
#N Attack, Attack Last Year
model_type <- "N Attack, N Attack Last Year"
coefs_1 <- ses_1 <- p_values_1 <- n_1 <- NULL
model  <- lm(trust_parliament ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model_type <- c(model_type, "Weapon and Attack FE, N Death")
model  <- lm(trust_parliament ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model_type <- c(model_type, "Int Terror Group")
model  <- lm(trust_parliament ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               international_attack_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model_type <- c(model_type, "Civilian Target")
model  <- lm(trust_parliament ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               civilian_target_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#All FE
model_type <- c(model_type, "All")
model  <- lm(trust_parliament ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               international_attack_1 + civilian_target_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model_type <- c(model_type, "N Attack, N Attack Last Year")
model  <- lm(trust_legal_system ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model_type <- c(model_type, "Weapon and Attack FE, N Death")
model  <- lm(trust_legal_system ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model_type <- c(model_type, "Int Terror Group")
model  <- lm(trust_legal_system ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               international_attack_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model_type <- c(model_type, "Civilian Target")
model  <- lm(trust_legal_system ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               civilian_target_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#All FE
model_type <- c(model_type, "All")
model  <- lm(trust_legal_system ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               international_attack_1 + civilian_target_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#5 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_5 <- ses_5 <- p_values_5 <- n_5 <- NULL
model  <- lm(trust_parliament ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_parliament ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_parliament ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               international_attack_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_parliament ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               civilian_target_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_parliament ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               international_attack_5 + civilian_target_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(trust_legal_system ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_legal_system ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_legal_system ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               international_attack_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_legal_system ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               civilian_target_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_legal_system ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               international_attack_5 + civilian_target_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)


#10 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_10 <- ses_10 <- p_values_10 <- n_10 <- NULL
model  <- lm(trust_parliament ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_parliament ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_parliament ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               international_attack_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_parliament ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               civilian_target_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_parliament ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               international_attack_10 + civilian_target_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(trust_legal_system ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_legal_system ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_legal_system ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               international_attack_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_legal_system ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               civilian_target_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_legal_system ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               international_attack_10 + civilian_target_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)


#20 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_20 <- ses_20 <- p_values_20 <- n_20 <- NULL
model  <- lm(trust_parliament ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_parliament ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_parliament ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               international_attack_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_parliament ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               civilian_target_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_parliament ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               international_attack_20 + civilian_target_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(trust_legal_system ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_legal_system ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_legal_system ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               international_attack_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_legal_system ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               civilian_target_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_legal_system ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               international_attack_20 + civilian_target_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)


#30 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_30 <- ses_30 <- p_values_30 <- n_30 <- NULL
model  <- lm(trust_parliament ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_parliament ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_parliament ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               international_attack_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_parliament ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               civilian_target_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_parliament ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               international_attack_30 + civilian_target_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(trust_legal_system ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_legal_system ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_legal_system ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               international_attack_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_legal_system ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               civilian_target_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_legal_system ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               international_attack_30 + civilian_target_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#TABLE TO PRESENT
p_values_1 <- ifelse(p_values_1 < 0.05 & p_values_1 > 0.01, "*",
                     ifelse(p_values_1 < 0.01, "**", ""))
p_values_5 <- ifelse(p_values_5 < 0.05 & p_values_5 > 0.01, "*",
                     ifelse(p_values_5 < 0.01, "**", ""))
p_values_10 <- ifelse(p_values_10 < 0.05 & p_values_10 > 0.01, "*",
                      ifelse(p_values_10 < 0.01, "**", ""))
p_values_20 <- ifelse(p_values_20 < 0.05 & p_values_20 > 0.01, "*",
                      ifelse(p_values_20 < 0.01, "**", ""))
p_values_30 <- ifelse(p_values_30 < 0.05 & p_values_30 > 0.01, "*",
                      ifelse(p_values_30 < 0.01, "**", ""))
coefs_1 <- str_c(round(coefs_1, 3), p_values_1)
coefs_5 <- str_c(round(coefs_5, 3), p_values_5)
coefs_10 <- str_c(round(coefs_10, 3), p_values_10)
coefs_20 <- str_c(round(coefs_20, 3), p_values_20)
coefs_30 <- str_c(round(coefs_30, 3), p_values_30)
ses_1 <- str_c("(", round(ses_1, 3), ")")
ses_5 <- str_c("(", round(ses_5, 3), ")")
ses_10 <- str_c("(", round(ses_10, 3), ")")
ses_20 <- str_c("(", round(ses_20, 3), ")")
ses_30 <- str_c("(", round(ses_30, 3), ")")
line_1 <- rep("Yes", 10)
line_2 <- rep("Yes", 10)
line_3 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_4 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_5 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_6 <- c(rep("No", 2), "Yes", "No", "Yes", rep("No", 2), "Yes", "No", "Yes")
line_7 <- c(rep("No", 3), rep("Yes", 2), rep("No", 3), rep("Yes", 2))

to_present <- rbind(coefs_1, ses_1, n_1, coefs_5, ses_5, n_5, coefs_10, ses_10, n_10, 
                    coefs_20, ses_20, n_20, coefs_30, ses_30, n_30, line_1, line_2,
                    line_3, line_4, line_5, line_6, line_7)

c_to_bind <- c("Success x Post (1 Day)", NA, "N", "Success x Post (5 Day)", NA, "N", 
               "Success x Post (10 Day)", NA, "N", "Success x Post (20 Day)", NA, "N", 
               "Success x Post (30 Day)", NA, "N", "N Attack", "N Attack Year", 
               "N Deaths", "Attack Type FE", "Weapon Type FE", "International Group", "Civilian Target")
to_present <- as.data.frame(cbind(c_to_bind, to_present))
colnames(to_present) <- c("", "(1)", "(2)", "(3)", "(4)", "(5)", "(6)", "(7)", "(8)", "(9)", "(10)")
kable(to_present,  row.names = FALSE)
```


```{r}
#################TABLE A7#################
rm(list = setdiff(ls(), "ess"))
#1 DAY
#LIFE SATISFACTION
#N Attack, Attack Last Year
model_type <- "N Attack, N Attack Last Year"
coefs_1 <- ses_1 <- p_values_1 <- n_1 <- NULL
model  <- lm(trust_police ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model_type <- c(model_type, "Weapon and Attack FE, N Death")
model  <- lm(trust_police ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model_type <- c(model_type, "Int Terror Group")
model  <- lm(trust_police ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               international_attack_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model_type <- c(model_type, "Civilian Target")
model  <- lm(trust_police ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               civilian_target_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#All FE
model_type <- c(model_type, "All")
model  <- lm(trust_police ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               international_attack_1 + civilian_target_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model_type <- c(model_type, "N Attack, N Attack Last Year")
model  <- lm(satisfaction_economy ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model_type <- c(model_type, "Weapon and Attack FE, N Death")
model  <- lm(satisfaction_economy ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model_type <- c(model_type, "Int Terror Group")
model  <- lm(satisfaction_economy ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               international_attack_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model_type <- c(model_type, "Civilian Target")
model  <- lm(satisfaction_economy ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               civilian_target_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#All FE
model_type <- c(model_type, "All")
model  <- lm(satisfaction_economy ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               international_attack_1 + civilian_target_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#5 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_5 <- ses_5 <- p_values_5 <- n_5 <- NULL
model  <- lm(trust_police ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_police ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_police ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               international_attack_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_police ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               civilian_target_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_police ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               international_attack_5 + civilian_target_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(satisfaction_economy ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_economy ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_economy ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               international_attack_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_economy ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               civilian_target_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_economy ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               international_attack_5 + civilian_target_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)


#10 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_10 <- ses_10 <- p_values_10 <- n_10 <- NULL
model  <- lm(trust_police ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_police ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_police ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               international_attack_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_police ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               civilian_target_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_police ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               international_attack_10 + civilian_target_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(satisfaction_economy ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_economy ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_economy ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               international_attack_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_economy ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               civilian_target_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_economy ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               international_attack_10 + civilian_target_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)


#20 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_20 <- ses_20 <- p_values_20 <- n_20 <- NULL
model  <- lm(trust_police ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_police ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_police ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               international_attack_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_police ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               civilian_target_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_police ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               international_attack_20 + civilian_target_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(satisfaction_economy ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_economy ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_economy ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               international_attack_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_economy ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               civilian_target_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_economy ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               international_attack_20 + civilian_target_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)


#30 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_30 <- ses_30 <- p_values_30 <- n_30 <- NULL
model  <- lm(trust_police ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(trust_police ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(trust_police ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               international_attack_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(trust_police ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               civilian_target_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(trust_police ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               international_attack_30 + civilian_target_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(satisfaction_economy ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(satisfaction_economy ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(satisfaction_economy ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               international_attack_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(satisfaction_economy ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               civilian_target_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(satisfaction_economy ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               international_attack_30 + civilian_target_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#TABLE TO PRESENT
p_values_1 <- ifelse(p_values_1 < 0.05 & p_values_1 > 0.01, "*",
                     ifelse(p_values_1 < 0.01, "**", ""))
p_values_5 <- ifelse(p_values_5 < 0.05 & p_values_5 > 0.01, "*",
                     ifelse(p_values_5 < 0.01, "**", ""))
p_values_10 <- ifelse(p_values_10 < 0.05 & p_values_10 > 0.01, "*",
                      ifelse(p_values_10 < 0.01, "**", ""))
p_values_20 <- ifelse(p_values_20 < 0.05 & p_values_20 > 0.01, "*",
                      ifelse(p_values_20 < 0.01, "**", ""))
p_values_30 <- ifelse(p_values_30 < 0.05 & p_values_30 > 0.01, "*",
                      ifelse(p_values_30 < 0.01, "**", ""))
coefs_1 <- str_c(round(coefs_1, 3), p_values_1)
coefs_5 <- str_c(round(coefs_5, 3), p_values_5)
coefs_10 <- str_c(round(coefs_10, 3), p_values_10)
coefs_20 <- str_c(round(coefs_20, 3), p_values_20)
coefs_30 <- str_c(round(coefs_30, 3), p_values_30)
ses_1 <- str_c("(", round(ses_1, 3), ")")
ses_5 <- str_c("(", round(ses_5, 3), ")")
ses_10 <- str_c("(", round(ses_10, 3), ")")
ses_20 <- str_c("(", round(ses_20, 3), ")")
ses_30 <- str_c("(", round(ses_30, 3), ")")
line_1 <- rep("Yes", 10)
line_2 <- rep("Yes", 10)
line_3 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_4 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_5 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_6 <- c(rep("No", 2), "Yes", "No", "Yes", rep("No", 2), "Yes", "No", "Yes")
line_7 <- c(rep("No", 3), rep("Yes", 2), rep("No", 3), rep("Yes", 2))

to_present <- rbind(coefs_1, ses_1, n_1, coefs_5, ses_5, n_5, coefs_10, ses_10, n_10, 
                    coefs_20, ses_20, n_20, coefs_30, ses_30, n_30, line_1, line_2,
                    line_3, line_4, line_5, line_6, line_7)

c_to_bind <- c("Success x Post (1 Day)", NA, "N", "Success x Post (5 Day)", NA, "N", 
               "Success x Post (10 Day)", NA, "N", "Success x Post (20 Day)", NA, "N", 
               "Success x Post (30 Day)", NA, "N", "N Attack", "N Attack Year", 
               "N Deaths", "Attack Type FE", "Weapon Type FE", "International Group", "Civilian Target")
to_present <- as.data.frame(cbind(c_to_bind, to_present))
colnames(to_present) <- c("", "(1)", "(2)", "(3)", "(4)", "(5)", "(6)", "(7)", "(8)", "(9)", "(10)")
kable(to_present,  row.names = FALSE)
```


```{r}
#################TABLE A8#################
rm(list = setdiff(ls(), "ess"))
#1 DAY
#LIFE SATISFACTION
#N Attack, Attack Last Year
model_type <- "N Attack, N Attack Last Year"
coefs_1 <- ses_1 <- p_values_1 <- n_1 <- NULL
model  <- lm(let_immigrants_different ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model_type <- c(model_type, "Weapon and Attack FE, N Death")
model  <- lm(let_immigrants_different ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model_type <- c(model_type, "Int Terror Group")
model  <- lm(let_immigrants_different ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               international_attack_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model_type <- c(model_type, "Civilian Target")
model  <- lm(let_immigrants_different ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               civilian_target_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#All FE
model_type <- c(model_type, "All")
model  <- lm(let_immigrants_different ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               international_attack_1 + civilian_target_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model_type <- c(model_type, "N Attack, N Attack Last Year")
model  <- lm(immigration_improves_country ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model_type <- c(model_type, "Weapon and Attack FE, N Death")
model  <- lm(immigration_improves_country ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model_type <- c(model_type, "Int Terror Group")
model  <- lm(immigration_improves_country ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               international_attack_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model_type <- c(model_type, "Civilian Target")
model  <- lm(immigration_improves_country ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +  
               civilian_target_1 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#All FE
model_type <- c(model_type, "All")
model  <- lm(immigration_improves_country ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
               attack_assault_1 + attack_assasination_1 + attack_bombing_1 + attack_facility_1 + attack_other_1 + 
               weapon_explosives_1 + weapon_firearms_1 + weapon_incendiary_1 + weapon_other_1 + n_deaths_1 +
               international_attack_1 + civilian_target_1 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_1 <- c(coefs_1, model$coefficients["success_1"])
ses_1 <- c(ses_1, coeftest(model, vcov_country)[2,2])
p_values_1 <- c(p_values_1, coeftest(model, vcov_country)[2,4])
n_1 <- c(n_1, nrow(model$model))
rm(model, vcov_country)

#5 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_5 <- ses_5 <- p_values_5 <- n_5 <- NULL
model  <- lm(let_immigrants_different ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(let_immigrants_different ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(let_immigrants_different ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               international_attack_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(let_immigrants_different ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               civilian_target_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(let_immigrants_different ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               international_attack_5 + civilian_target_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(immigration_improves_country ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(immigration_improves_country ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(immigration_improves_country ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               international_attack_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(immigration_improves_country ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year +  
               civilian_target_5 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(immigration_improves_country ~ success_5 + post_5 + success_unit_5 + attack_number_5 + attack_last_year + 
               attack_assault_5 + attack_assasination_5 + attack_bombing_5 + attack_facility_5 + attack_other_5 + 
               weapon_explosives_5 + weapon_firearms_5 + weapon_incendiary_5 + weapon_other_5 + n_deaths_5 +
               international_attack_5 + civilian_target_5 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_5 <- c(coefs_5, model$coefficients["success_5"])
ses_5 <- c(ses_5, coeftest(model, vcov_country)[2,2])
p_values_5 <- c(p_values_5, coeftest(model, vcov_country)[2,4])
n_5 <- c(n_5, nrow(model$model))
rm(model, vcov_country)


#10 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_10 <- ses_10 <- p_values_10 <- n_10 <- NULL
model  <- lm(let_immigrants_different ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(let_immigrants_different ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(let_immigrants_different ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               international_attack_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(let_immigrants_different ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               civilian_target_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(let_immigrants_different ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               international_attack_10 + civilian_target_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(immigration_improves_country ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(immigration_improves_country ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(immigration_improves_country ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               international_attack_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(immigration_improves_country ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year +  
               civilian_target_10 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(immigration_improves_country ~ success_10 + post_10 + success_unit_10 + attack_number_10 + attack_last_year + 
               attack_assault_10 + attack_assasination_10 + attack_bombing_10 + attack_facility_10 + attack_other_10 + 
               weapon_explosives_10 + weapon_firearms_10 + weapon_incendiary_10 + weapon_other_10 + n_deaths_10 +
               international_attack_10 + civilian_target_10 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_10 <- c(coefs_10, model$coefficients["success_10"])
ses_10 <- c(ses_10, coeftest(model, vcov_country)[2,2])
p_values_10 <- c(p_values_10, coeftest(model, vcov_country)[2,4])
n_10 <- c(n_10, nrow(model$model))
rm(model, vcov_country)


#20 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_20 <- ses_20 <- p_values_20 <- n_20 <- NULL
model  <- lm(let_immigrants_different ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(let_immigrants_different ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(let_immigrants_different ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               international_attack_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(let_immigrants_different ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               civilian_target_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(let_immigrants_different ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               international_attack_20 + civilian_target_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(immigration_improves_country ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(immigration_improves_country ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(immigration_improves_country ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               international_attack_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(immigration_improves_country ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year +  
               civilian_target_20 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(immigration_improves_country ~ success_20 + post_20 + success_unit_20 + attack_number_20 + attack_last_year + 
               attack_assault_20 + attack_assasination_20 + attack_bombing_20 + attack_facility_20 + attack_other_20 + 
               weapon_explosives_20 + weapon_firearms_20 + weapon_incendiary_20 + weapon_other_20 + n_deaths_20 +
               international_attack_20 + civilian_target_20 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_20 <- c(coefs_20, model$coefficients["success_20"])
ses_20 <- c(ses_20, coeftest(model, vcov_country)[2,2])
p_values_20 <- c(p_values_20, coeftest(model, vcov_country)[2,4])
n_20 <- c(n_20, nrow(model$model))
rm(model, vcov_country)


#30 DAYS
#LIFE SATISFACTION
#N Attack, Attack Last Year
coefs_30 <- ses_30 <- p_values_30 <- n_30 <- NULL
model  <- lm(let_immigrants_different ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(let_immigrants_different ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(let_immigrants_different ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               international_attack_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(let_immigrants_different ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               civilian_target_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(let_immigrants_different ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               international_attack_30 + civilian_target_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#GOVERNMENT SATISFACTION
#N Attack, Attack Last Year
model  <- lm(immigration_improves_country ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Weapon and Attack FE, N death
model  <- lm(immigration_improves_country ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#International Terrorist Group
model  <- lm(immigration_improves_country ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               international_attack_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#Civilian Target
model  <- lm(immigration_improves_country ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year +  
               civilian_target_30 + as.factor(country) + as.factor(year), 
             data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#All FE
model  <- lm(immigration_improves_country ~ success_30 + post_30 + success_unit_30 + attack_number_30 + attack_last_year + 
               attack_assault_30 + attack_assasination_30 + attack_bombing_30 + attack_facility_30 + attack_other_30 + 
               weapon_explosives_30 + weapon_firearms_30 + weapon_incendiary_30 + weapon_other_30 + n_deaths_30 +
               international_attack_30 + civilian_target_30 +
               as.factor(country) + as.factor(year), data = ess, weights = post_stratification_weights)
vcov_country <- cluster.vcov(model, ess$country)
coefs_30 <- c(coefs_30, model$coefficients["success_30"])
ses_30 <- c(ses_30, coeftest(model, vcov_country)[2,2])
p_values_30 <- c(p_values_30, coeftest(model, vcov_country)[2,4])
n_30 <- c(n_30, nrow(model$model))
rm(model, vcov_country)

#TABLE TO PRESENT
p_values_1 <- ifelse(p_values_1 < 0.05 & p_values_1 > 0.01, "*",
                     ifelse(p_values_1 < 0.01, "**", ""))
p_values_5 <- ifelse(p_values_5 < 0.05 & p_values_5 > 0.01, "*",
                     ifelse(p_values_5 < 0.01, "**", ""))
p_values_10 <- ifelse(p_values_10 < 0.05 & p_values_10 > 0.01, "*",
                      ifelse(p_values_10 < 0.01, "**", ""))
p_values_20 <- ifelse(p_values_20 < 0.05 & p_values_20 > 0.01, "*",
                      ifelse(p_values_20 < 0.01, "**", ""))
p_values_30 <- ifelse(p_values_30 < 0.05 & p_values_30 > 0.01, "*",
                      ifelse(p_values_30 < 0.01, "**", ""))
coefs_1 <- str_c(round(coefs_1, 3), p_values_1)
coefs_5 <- str_c(round(coefs_5, 3), p_values_5)
coefs_10 <- str_c(round(coefs_10, 3), p_values_10)
coefs_20 <- str_c(round(coefs_20, 3), p_values_20)
coefs_30 <- str_c(round(coefs_30, 3), p_values_30)
ses_1 <- str_c("(", round(ses_1, 3), ")")
ses_5 <- str_c("(", round(ses_5, 3), ")")
ses_10 <- str_c("(", round(ses_10, 3), ")")
ses_20 <- str_c("(", round(ses_20, 3), ")")
ses_30 <- str_c("(", round(ses_30, 3), ")")
line_1 <- rep("Yes", 10)
line_2 <- rep("Yes", 10)
line_3 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_4 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_5 <- c("No", "Yes", rep("No", 2), "Yes","No", "Yes", rep("No", 2), "Yes")
line_6 <- c(rep("No", 2), "Yes", "No", "Yes", rep("No", 2), "Yes", "No", "Yes")
line_7 <- c(rep("No", 3), rep("Yes", 2), rep("No", 3), rep("Yes", 2))

to_present <- rbind(coefs_1, ses_1, n_1, coefs_5, ses_5, n_5, coefs_10, ses_10, n_10, 
                    coefs_20, ses_20, n_20, coefs_30, ses_30, n_30, line_1, line_2,
                    line_3, line_4, line_5, line_6, line_7)

c_to_bind <- c("Success x Post (1 Day)", NA, "N", "Success x Post (5 Day)", NA, "N", 
               "Success x Post (10 Day)", NA, "N", "Success x Post (20 Day)", NA, "N", 
               "Success x Post (30 Day)", NA, "N", "N Attack", "N Attack Year", 
               "N Deaths", "Attack Type FE", "Weapon Type FE", "International Group", "Civilian Target")
to_present <- as.data.frame(cbind(c_to_bind, to_present))
colnames(to_present) <- c("", "(1)", "(2)", "(3)", "(4)", "(5)", "(6)", "(7)", "(8)", "(9)", "(10)")
kable(to_present,  row.names = FALSE)
```

```{r fig.height = 14, fig.width = 12, fig.align = "center", message=FALSE, warning=FALSE}
############FIGURE A3 AND A4#####################
rm(list = setdiff(ls(), "ess"))
#LIFE SATISFACTION
these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("satisfaction_life", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(satisfaction_life ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(satisfaction_life ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]



# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_life <- plotdat

#GOVERNMENT SATISFACTION
rm(list = setdiff(ls(), c("ess", "plotdat_life")))
these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("satisfaction_government", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(satisfaction_government ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(satisfaction_government ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_gov <- plotdat

#TRUST PARLIAMENT
rm(list = setdiff(ls(), c("ess", "plotdat_life", "plotdat_gov")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("trust_parliament", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(trust_parliament ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(trust_parliament ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_parliament <- plotdat

#TRUST LEGAL SYSTEM
rm(list = setdiff(ls(), c("ess", "plotdat_life", "plotdat_gov", "plotdat_parliament")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("trust_legal_system", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(trust_legal_system ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(trust_legal_system ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_legal <- plotdat

#TRUST POLICE
rm(list = setdiff(ls(), c("ess", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("trust_police", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(trust_police ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(trust_police ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_police <- plotdat

#SATISFACTION WITH ECONOMY
rm(list = setdiff(ls(), c("ess", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("satisfaction_economy", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(satisfaction_economy ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(satisfaction_economy ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_economy <- plotdat

#ACCEPT IMMIGRANTS FROM DIFFERENT RACE
rm(list = setdiff(ls(), c("ess", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police", "plotdat_economy")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("let_immigrants_different", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(let_immigrants_different ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(let_immigrants_different ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_different <- plotdat

#IMMIGRATION IMPROVES COUNTRY
rm(list = setdiff(ls(), c("ess", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police", "plotdat_economy", "plotdat_different")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("immigration_improves_country", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(immigration_improves_country ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(immigration_improves_country ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_immigration <- plotdat

rm(list = setdiff(ls(), c("ess", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police", "plotdat_economy", "plotdat_different", "plotdat_immigration")))

#Merge all
plotdat_life$variable <- "Life satisfaction"
plotdat_gov$variable <- "Government satisfaction"
plotdat_parliament$variable <- "Trust parliament"
plotdat_legal$variable <- "Trust legal system"
plotdat_police$variable <- "Trust police"
plotdat_economy$variable <- "Satisfaction with economy"
plotdat_different$variable <- "Accept immigrants \n from different race"
plotdat_immigration$variable <- "Immigration is beneficial"
plotdat <- rbind(plotdat_life, plotdat_gov, plotdat_parliament,
                 plotdat_legal, plotdat_police, plotdat_economy,
                 plotdat_different, plotdat_immigration)

plotdat$country <- rownames(plotdat)
plotdat$country <- str_remove_all(plotdat$country, "1")
plotdat$country <- str_remove_all(plotdat$country, "2")
plotdat$country <- str_remove_all(plotdat$country, "3")
plotdat$country <- str_remove_all(plotdat$country, "4")
plotdat$country <- str_remove_all(plotdat$country, "5")
plotdat$country <- str_remove_all(plotdat$country, "6")
plotdat$country <- str_remove_all(plotdat$country, "7")
plotdat$variable <- factor(plotdat$variable, levels = c("Life satisfaction", "Government satisfaction", 
                                                        "Trust parliament", "Trust legal system", 
                                                        "Trust police", "Satisfaction with economy", 
                                                        "Accept immigrants \n from different race", 
                                                        "Immigration is beneficial"))


ggplot(plotdat[plotdat$variable %in% c("Life satisfaction", "Government satisfaction",
                                         "Trust parliament", "Trust legal system"),], aes(ranefs, country)) +  
  theme_classic() +
  ylab("") + xlab("") + 
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_vline(xintercept = 0, color = "red", linetype = "dotted" ) +
  geom_errorbarh(aes(xmin = ranefs.lo.50, xmax = ranefs.hi.50, height = .01), size = 1.5) +
  geom_errorbarh(aes(xmin = ranefs.lo.95, xmax = ranefs.hi.95, height = .01), size = .5) +
  facet_wrap(~variable, ncol = 2) +
  theme(legend.position="none", plot.margin = margin(0.5, 0.6, 0.5, 0, "cm"), 
        axis.text.y =  element_text(size = 22), axis.text.x = element_text(size = 18), 
        axis.title.y =  element_text(size = 34), axis.title.x = element_text(size = 34), 
        plot.title = element_text(hjust = 0.5, size = 22),
        strip.text.x = element_text(size = 28),
        strip.background = element_rect(fill='grey', color='white'))


ggplot(plotdat[!plotdat$variable %in% c("Life satisfaction", "Government satisfaction",
                                         "Trust parliament", "Trust legal system"),], aes(ranefs, country)) +  
  theme_classic() +
  ylab("") + xlab("") + 
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_vline(xintercept = 0, color = "red", linetype = "dotted" ) +
  geom_errorbarh(aes(xmin = ranefs.lo.50, xmax = ranefs.hi.50, height = .01), size = 1.5) +
  geom_errorbarh(aes(xmin = ranefs.lo.95, xmax = ranefs.hi.95, height = .01), size = .5) +
  facet_wrap(~variable, ncol = 2) +
  theme(legend.position="none", plot.margin = margin(0.5, 0.6, 0.5, 0, "cm"), 
        axis.text.y =  element_text(size = 22), axis.text.x = element_text(size = 18), 
        axis.title.y =  element_text(size = 34), axis.title.x = element_text(size = 34), 
        plot.title = element_text(hjust = 0.5, size = 22),
        strip.text.x = element_text(size = 28),
        strip.background = element_rect(fill='grey', color='white'))
rm(list = setdiff(ls(), "ess"))

```

```{r fig.height = 20, fig.width = 12, fig.align = "center", message=FALSE, warning=FALSE}
##############FIGURE A5#################
dv <- c("let_immigrants_different", 
        "satisfaction_life", "satisfaction_government", 
        "satisfaction_economy", "trust_parliament", 
        "trust_legal_system", "trust_police", 
        "immigration_improves_country")
coefs <- ses <- variables <- coefs_right <- ses_right <- coefs_int <- ses_int <- variables <- NULL
days <- c(1:15, 20, 30)
for(i in 1:length(dv)){
  for(k in 1:17){
    day.formula <- as.formula(paste(paste(dv[i]), paste("~ success_", days[k], sep = ""), paste("* left_right_scale"), paste("+ post_", days[k], sep =""),  
                                    paste("+ success_unit_", days[k], sep = ""),  paste("+ attack_number_", days[k], sep = ""), 
                                    "+ attack_last_year + as.factor(country) + as.factor(year)", collapse=''))
    
    model  <- lm(day.formula, data = ess, weights = post_stratification_weights)
    vcov_country <- cluster.vcov(model, ess$country)
    model_coefs <- coeftest(model, vcov_country)
    coefs <- c(coefs, model_coefs[2, 1])
    ses <- c(ses, model_coefs[2,2])
    coefs_right <- c(coefs_right, model_coefs[3,1])
    ses_right <- c(ses_right, model_coefs[3,2])
    coefs_int <- c(coefs_int, model_coefs[nrow(model_coefs),1])
    ses_int <- c(ses_int, model_coefs[nrow(model_coefs),2])
    variables <- c(variables, dv[i])
    rm(day.formula, model, vcov_country)
  }
  print(i)
}

to_plot <- data.frame(coefs, coefs_right, coefs_int, ses, ses_right, ses_int, days = c(1:15, 20, 30), variable  = c(rep("Life satisfaction", 17), rep("Government satisfaction", 17)))
to_plot$coefs_lower <- to_plot$coefs - to_plot$ses*1.96
to_plot$coefs_upper <- to_plot$coefs + to_plot$ses*1.96

to_plot$coefs_right_lower <- to_plot$coefs_right - to_plot$ses_right*1.96
to_plot$coefs_right_upper <- to_plot$coefs_right + to_plot$ses_right*1.96

to_plot$coefs_int_lower <- to_plot$coefs_int - to_plot$ses_int*1.96
to_plot$coefs_int_upper <- to_plot$coefs_int + to_plot$ses_int*1.96

to_plot$right_effect <- to_plot$coefs + to_plot$coefs_int*7
to_plot$right_effect_lower <- to_plot$coefs_lower + to_plot$coefs_int_lower*7
to_plot$right_effect_upper <- to_plot$coefs_upper + to_plot$coefs_int_upper*7

to_plot$left_effect <- to_plot$coefs +  to_plot$coefs_int*3
to_plot$left_effect_lower <- to_plot$coefs_lower + to_plot$coefs_int_lower*3
to_plot$left_effect_upper <- to_plot$coefs_upper + to_plot$coefs_int_lower*3

plot_data <- data.frame(effect = c(to_plot$right_effect, to_plot$left_effect),
                        lower = c(to_plot$right_effect_lower, to_plot$left_effect_lower),
                        upper = c(to_plot$right_effect_upper, to_plot$left_effect_upper),
                        days = rep(c(1:17), 16), 
                        type = c(rep("Right", 136), rep("Left", 136)),
                        variables = rep(c(rep("Accept immigrants \n from different race", 17), rep("Life satisfaction", 17),
                                          rep("Government satisfaction", 17), rep("Satisfaction with economy", 17),
                                          rep("Trust parliament", 17), rep("Trust legal system", 17), 
                                          rep("Trust police", 17), rep("Immigration is beneficial", 17)),2))

plot_data$variables <- factor(plot_data$variables, levels = c("Life satisfaction","Government satisfaction",
                                                              "Trust parliament", "Trust legal system",
                                                              "Trust police", "Satisfaction with economy",
                                                              "Accept immigrants \n from different race", 
                                                              "Immigration is beneficial"))


ggplot(plot_data, aes(days, effect)) + 
  geom_line(aes(colour = as.factor(type))) + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
  theme_classic() +
  facet_wrap(~variables, ncol = 2) +
  geom_point(aes(size=1.2, color = as.factor(type))) +
  labs(x = "Number of Days After Attack", y = "Effect", title = "") +
  scale_x_continuous(breaks = c(1:17), labels = c(1:15, 20, 30)) +
  theme(legend.justification=c(0,0), legend.position = "bottom", legend.title = element_blank(),
        legend.text = element_text(size = 28),
        plot.margin = margin(0.5, 0.6, 0.5, 0, "cm"), 
        axis.text.y =  element_text(size = 22), axis.text.x = element_text(size = 18), 
        axis.title.y =  element_text(size = 34), axis.title.x = element_text(size = 34), 
        plot.title = element_text(hjust = 0.5, size = 22),
        strip.text.x = element_text(size = 30),
        strip.background = element_rect(fill='grey', color='white')) +
  guides(size = FALSE) +
  geom_ribbon(aes(ymin=lower, ymax=upper, fill = as.factor(type)),  alpha=0.2) + 
  scale_color_manual(values=c("grey70", "grey10")) +
  scale_fill_manual(values=c("grey70", "grey10")) 
rm(list = setdiff(ls(), "ess"))
```


```{r fig.height = 14, fig.width = 12, fig.align = "center", message=FALSE, warning=FALSE}
#################FIGURE A6 AND A7###################
ess_original <- ess
ess <- ess[ess$left_right_scale < 6 & !is.na(ess$left_right_scale),]

#LIFE SATISFACTION
these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("satisfaction_life", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(satisfaction_life ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(satisfaction_life ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]



# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_life <- plotdat

#GOVERNMENT SATISFACTION
rm(list = setdiff(ls(), c("ess", "plotdat_life", "ess_original")))
these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("satisfaction_government", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(satisfaction_government ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(satisfaction_government ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_gov <- plotdat

#TRUST PARLIAMENT
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("trust_parliament", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(trust_parliament ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(trust_parliament ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_parliament <- plotdat

#TRUST LEGAL SYSTEM
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "plotdat_parliament")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("trust_legal_system", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(trust_legal_system ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(trust_legal_system ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_legal <- plotdat

#TRUST POLICE
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("trust_police", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(trust_police ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(trust_police ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_police <- plotdat

#SATISFACTION WITH ECONOMY
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("satisfaction_economy", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(satisfaction_economy ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(satisfaction_economy ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_economy <- plotdat

#ACCEPT IMMIGRANTS FROM DIFFERENT RACE
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police", "plotdat_economy")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("let_immigrants_different", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(let_immigrants_different ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(let_immigrants_different ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_different <- plotdat

#IMMIGRATION IMPROVES COUNTRY
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police", "plotdat_economy", "plotdat_different")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("immigration_improves_country", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(immigration_improves_country ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(immigration_improves_country ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_immigration <- plotdat

rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police", "plotdat_economy", "plotdat_different", "plotdat_immigration")))

#Merge all
plotdat_life$variable <- "Life satisfaction"
plotdat_gov$variable <- "Government satisfaction"
plotdat_parliament$variable <- "Trust parliament"
plotdat_legal$variable <- "Trust legal system"
plotdat_police$variable <- "Trust police"
plotdat_economy$variable <- "Satisfaction with economy"
plotdat_different$variable <- "Accept immigrants \n from different race"
plotdat_immigration$variable <- "Immigration is beneficial"
plotdat <- rbind(plotdat_life, plotdat_gov, plotdat_parliament,
                 plotdat_legal, plotdat_police, plotdat_economy,
                 plotdat_different, plotdat_immigration)

plotdat$country <- rownames(plotdat)
plotdat$country <- str_remove_all(plotdat$country, "1")
plotdat$country <- str_remove_all(plotdat$country, "2")
plotdat$country <- str_remove_all(plotdat$country, "3")
plotdat$country <- str_remove_all(plotdat$country, "4")
plotdat$country <- str_remove_all(plotdat$country, "5")
plotdat$country <- str_remove_all(plotdat$country, "6")
plotdat$country <- str_remove_all(plotdat$country, "7")
plotdat$variable <- factor(plotdat$variable, levels = c("Life satisfaction", "Government satisfaction", 
                                                        "Trust parliament", "Trust legal system", 
                                                        "Trust police", "Satisfaction with economy", 
                                                        "Accept immigrants \n from different race", 
                                                        "Immigration is beneficial"))

for_left <- plotdat
rm(list = setdiff(ls(), c("ess_original", "for_left")))

# FOR RIGHT
ess <- ess_original[ess_original$left_right_scale >5 & !is.na(ess_original$left_right_scale),]

#LIFE SATISFACTION
these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("satisfaction_life", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(satisfaction_life ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(satisfaction_life ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]



# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_life <- plotdat

#GOVERNMENT SATISFACTION
rm(list = setdiff(ls(), c("ess", "plotdat_life", "ess_original", "for_left")))
these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("satisfaction_government", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(satisfaction_government ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(satisfaction_government ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_gov <- plotdat

#TRUST PARLIAMENT
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "for_left")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("trust_parliament", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(trust_parliament ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(trust_parliament ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_parliament <- plotdat

#TRUST LEGAL SYSTEM
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "plotdat_parliament", "for_left")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("trust_legal_system", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(trust_legal_system ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(trust_legal_system ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_legal <- plotdat

#TRUST POLICE
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "for_left")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("trust_police", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(trust_police ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(trust_police ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_police <- plotdat

#SATISFACTION WITH ECONOMY
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police", "for_left")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("satisfaction_economy", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(satisfaction_economy ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(satisfaction_economy ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_economy <- plotdat

#ACCEPT IMMIGRANTS FROM DIFFERENT RACE
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police", "plotdat_economy", "for_left")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("let_immigrants_different", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(let_immigrants_different ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(let_immigrants_different ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_different <- plotdat

#IMMIGRATION IMPROVES COUNTRY
rm(list = setdiff(ls(), c("ess", "ess_original", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police", "plotdat_economy", "plotdat_different", "for_left")))

these.ranef <- NULL

# create df with only relevant variables, otherwise the loop below is too slow

ess.s <- ess[, c("immigration_improves_country", "success_1", "post_1", 'success_unit_1', 'attack_number_1', 'attack_last_year',
                 'year', 'country','post_stratification_weights')]
ess.s <- ess.s[!is.na(ess.s$success_1), ]

# Bootstrap the regression, collect random effects into "these.ranef"
set.seed(123)
for(boot in 1:1000){
  
  if(boot%%10==0) print(boot)
  
  sample1 <- sample(1:nrow(ess.s), replace = TRUE)
  ess.boot <- ess.s[sample1, ]
  
  
  
  model.lmer.slope.boot  <- lmer(immigration_improves_country ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year +
                                   as.factor(year) + (1+success_1 | country), data = ess.boot, weights = post_stratification_weights)
  
  
  ref <- ranef(model.lmer.slope.boot)
  this.ranef <- ref$country$success_1
  these.ranef <- cbind(these.ranef, this.ranef)
  
}

model.lmer.slope  <- lmer(immigration_improves_country ~ success_1 + post_1 + success_unit_1 + attack_number_1 + attack_last_year + 
                            as.factor(year) + (1 + success_1 | country), data = ess, weights = post_stratification_weights)

ref <- ranef(model.lmer.slope)
ranefs <-  ref$country$success_1 +(summary(model.lmer.slope)$coefficients)[2,1]


#CIs
ranefs.hi.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.975)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.95 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.025)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.hi.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.75)) +summary(model.lmer.slope)$coefficients[2,1]
ranefs.lo.50 <-  apply(these.ranef, MARGIN = 1, FUN=function(x)quantile(x, probs = 0.25)) +summary(model.lmer.slope)$coefficients[2,1]

# Gather the results in a df
plotdat <- data.frame(ranefs, ranefs.hi.95, ranefs.lo.95, ranefs.hi.50, ranefs.lo.50)
row.names(plotdat) <- row.names(ref$country)

plotdat_immigration <- plotdat
ess <- ess_original

rm(list = setdiff(ls(), c("ess", "plotdat_life", "plotdat_gov", "plotdat_parliament", "plotdat_legal", "plotdat_police", "plotdat_economy", "plotdat_different", "plotdat_immigration", "for_left")))

#Merge all
plotdat_life$variable <- "Life satisfaction"
plotdat_gov$variable <- "Government satisfaction"
plotdat_parliament$variable <- "Trust parliament"
plotdat_legal$variable <- "Trust legal system"
plotdat_police$variable <- "Trust police"
plotdat_economy$variable <- "Satisfaction with economy"
plotdat_different$variable <- "Accept immigrants \n from different race"
plotdat_immigration$variable <- "Immigration is beneficial"
plotdat <- rbind(plotdat_life, plotdat_gov, plotdat_parliament,
                 plotdat_legal, plotdat_police, plotdat_economy,
                 plotdat_different, plotdat_immigration)

plotdat$country <- rownames(plotdat)
plotdat$country <- str_remove_all(plotdat$country, "1")
plotdat$country <- str_remove_all(plotdat$country, "2")
plotdat$country <- str_remove_all(plotdat$country, "3")
plotdat$country <- str_remove_all(plotdat$country, "4")
plotdat$country <- str_remove_all(plotdat$country, "5")
plotdat$country <- str_remove_all(plotdat$country, "6")
plotdat$country <- str_remove_all(plotdat$country, "7")
plotdat$variable <- factor(plotdat$variable, levels = c("Life satisfaction", "Government satisfaction", 
                                                        "Trust parliament", "Trust legal system", 
                                                        "Trust police", "Satisfaction with economy", 
                                                        "Accept immigrants \n from different race", 
                                                        "Immigration is beneficial"))


#combine both left and right
for_left$ideology <- "Left"
plotdat$ideology <- "Right"
plotdat <- rbind(for_left, plotdat)


ggplot(plotdat[plotdat$variable %in% c("Life satisfaction", "Government satisfaction",
                                         "Trust parliament", "Trust legal system"),], aes(ranefs, country, color = ideology, group = ideology)) +  
  theme_classic() +
  ylab("") + xlab("") + 
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_vline(xintercept = 0, color = "red", linetype = "dotted" ) +
  geom_errorbarh(aes(xmin = ranefs.lo.50, xmax = ranefs.hi.50, height = .01), size = 1.5,  position=position_dodge(0.5)) +
  geom_errorbarh(aes(xmin = ranefs.lo.95, xmax = ranefs.hi.95, height = .01), size = .5, position=position_dodge(0.5)) +
  facet_wrap(~variable, ncol = 2)+
  scale_color_manual(values=c("red", "blue"))+
  theme(legend.position="none", plot.margin = margin(0.5, 0.6, 0.5, 0, "cm"), 
        axis.text.y =  element_text(size = 22), axis.text.x = element_text(size = 18), 
        axis.title.y =  element_text(size = 34), axis.title.x = element_text(size = 34), 
        plot.title = element_text(hjust = 0.5, size = 22),
        strip.text.x = element_text(size = 28),
        strip.background = element_rect(fill='grey', color='white'))
  

ggplot(plotdat[!plotdat$variable %in% c("Life satisfaction", "Government satisfaction",
                                         "Trust parliament", "Trust legal system"),], 
         aes(ranefs, country, color = ideology, group = ideology)) +  
  theme_classic() +
  ylab("") + xlab("") + 
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_vline(xintercept = 0, color = "red", linetype = "dotted" ) +
  geom_errorbarh(aes(xmin = ranefs.lo.50, xmax = ranefs.hi.50, height = .01), size = 1.5,  position=position_dodge(0.5)) +
  geom_errorbarh(aes(xmin = ranefs.lo.95, xmax = ranefs.hi.95, height = .01), size = .5, position=position_dodge(0.5)) +
  facet_wrap(~variable, ncol = 2)+
  scale_color_manual(values=c("red", "blue"))+
  theme(legend.position="none", plot.margin = margin(0.5, 0.6, 0.5, 0, "cm"), 
        axis.text.y =  element_text(size = 22), axis.text.x = element_text(size = 18), 
        axis.title.y =  element_text(size = 34), axis.title.x = element_text(size = 34), 
        plot.title = element_text(hjust = 0.5, size = 22),
        strip.text.x = element_text(size = 28),
        strip.background = element_rect(fill='grey', color='white'))
rm(list = setdiff(ls(), "ess"))
```

```{r fig.height = 20, fig.width = 12, fig.align = "center", message=FALSE, warning=FALSE}
#################FIGURE A8###################
#GOVERNMENT SUPPORT INTERACTION####
dv <- c("let_immigrants_different", 
        "satisfaction_life", "satisfaction_government", 
        "satisfaction_economy", "trust_parliament", 
        "trust_legal_system", "trust_police", 
        "immigration_improves_country")
coefs <- ses <- variables <- coefs_gov <- ses_gov <- coefs_int <- ses_int <- variables <- NULL
days <- c(1:15, 20, 30)
for(i in 1:length(dv)){
  for(k in 1:17){
    day.formula <- as.formula(paste(paste(dv[i]), paste("~ success_", days[k], sep = ""), paste("* government_party"), paste("+ post_", days[k], sep =""),  
                                    paste("+ success_unit_", days[k], sep = ""),  paste("+ attack_number_", days[k], sep = ""), 
                                    "+ attack_last_year + as.factor(country) + as.factor(year)", collapse=''))
    
    model  <- lm(day.formula, data = ess, weights = post_stratification_weights)
    vcov_country <- cluster.vcov(model, ess$country)
    model_coefs <- coeftest(model, vcov_country)
    coefs <- c(coefs, model_coefs[2, 1])
    ses <- c(ses, model_coefs[2,2])
    coefs_gov <- c(coefs_gov, model_coefs[3,1])
    ses_gov <- c(ses_gov, model_coefs[3,2])
    coefs_int <- c(coefs_int, model_coefs[nrow(model_coefs),1])
    ses_int <- c(ses_int, model_coefs[nrow(model_coefs),2])
    variables <- c(variables, dv[i])
    rm(day.formula, model, vcov_country)
  }
  print(i)
}

to_plot <- data.frame(coefs, coefs_gov, coefs_int, ses, ses_gov, ses_int, days = c(1:15, 20, 30), variable  = c(rep("Life satisfaction", 17), rep("Government satisfaction", 17)))
to_plot$coefs_lower <- to_plot$coefs - to_plot$ses*1.96
to_plot$coefs_upper <- to_plot$coefs + to_plot$ses*1.96

to_plot$coefs_gov_lower <- to_plot$coefs_gov - to_plot$ses_gov*1.96
to_plot$coefs_gov_upper <- to_plot$coefs_gov + to_plot$ses_gov*1.96

to_plot$coefs_int_lower <- to_plot$coefs_int - to_plot$ses_int*1.96
to_plot$coefs_int_upper <- to_plot$coefs_int + to_plot$ses_int*1.96

to_plot$gov_effect <- to_plot$coefs + to_plot$coefs_int
to_plot$gov_effect_lower <- to_plot$coefs_lower + to_plot$coefs_int_lower
to_plot$gov_effect_upper <- to_plot$coefs_upper + to_plot$coefs_int_upper

to_plot$no_gov_effect <- to_plot$coefs 
to_plot$no_gov_effect_lower <- to_plot$coefs_lower 
to_plot$no_gov_effect_upper <- to_plot$coefs_upper 

plot_data <- data.frame(effect = c(to_plot$gov_effect, to_plot$no_gov_effect),
                        lower = c(to_plot$gov_effect_lower, to_plot$no_gov_effect_lower),
                        upper = c(to_plot$gov_effect_upper, to_plot$no_gov_effect_upper),
                        days = rep(c(1:17), 16), 
                        type = c(rep("Government support", 136), rep("No government support", 136)),
                        variables = rep(c(rep("Accept immigrants \n from different race", 17), rep("Life satisfaction", 17),
                                          rep("Government satisfaction", 17), rep("Satisfaction with economy", 17),
                                          rep("Trust parliament", 17), rep("Trust legal system", 17), 
                                          rep("Trust police", 17), rep("Immigration is beneficial", 17)),2))
plot_data$variables <- factor(plot_data$variables, levels = c("Life satisfaction","Government satisfaction",
                                                              "Trust parliament", "Trust legal system",
                                                              "Trust police", "Satisfaction with economy",
                                                              "Accept immigrants \n from different race", 
                                                              "Immigration is beneficial"))


ggplot(plot_data, aes(days, effect)) + 
  geom_line(aes(colour = as.factor(type))) + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
  theme_classic() +
  facet_wrap(~variables, ncol = 2) +
  geom_point(aes(size=1.2, color = as.factor(type))) +
  labs(x = "Number of Days After Attack", y = "Effect", title = "") +
  scale_x_continuous(breaks = c(1:17), labels = c(1:15, 20, 30)) +
  theme(legend.justification=c(0,0), legend.position = "bottom", legend.title = element_blank(),
        legend.text = element_text(size = 28),
        plot.margin = margin(0.5, 0.6, 0.5, 0, "cm"), 
        axis.text.y =  element_text(size = 22), axis.text.x = element_text(size = 18), 
        axis.title.y =  element_text(size = 34), axis.title.x = element_text(size = 34), 
        plot.title = element_text(hjust = 0.5, size = 22),
        strip.text.x = element_text(size = 30),
        strip.background = element_rect(fill='grey', color='white')) +
  guides(size=FALSE)+
  geom_ribbon(aes(ymin=lower, ymax=upper, fill = as.factor(type)),  alpha=0.2) + 
  scale_color_manual(values=c("grey80", "grey20")) +
  scale_fill_manual(values=c("grey80", "grey30"))
```

```{r}
###############TABLE A9#################
rm(list = ls())
load("gtrends.RData")
#ols model general
model1 <- lm(hits ~ success + fail + as.factor(this.geo) + as.factor(year), data = attack)

#clustering SE
vcov_country_1 <- cluster.vcov(model1, attack$this.geo)
coefs1 <- coeftest(model1, vcov_country_1)

#success vs fail
model2 <- lm(hits ~ success + fail + as.factor(this.geo) + as.factor(year), data = attack[attack$successful_attack > -1,])

#clustering SE
vcov_country_2 <- cluster.vcov(model2, attack$this.geo[attack$successful_attack > -1])
coefs2 <- coeftest(model2, vcov_country_2)

#fail vs no attack
model3 <- lm(hits ~ success + fail + as.factor(this.geo) + as.factor(year), data = attack[attack$successful_attack < 1,])

#clustering SE
vcov_country_3 <- cluster.vcov(model3, attack$this.geo[attack$successful_attack < 1])
coefs3 <- coeftest(model3, vcov_country_3)

#success vs no attack
model4 <- lm(hits ~ success + fail + as.factor(this.geo) + as.factor(year), data = attack[attack$successful_attack %in% c(-1, 1),])

#clustering SE
vcov_country_4 <- cluster.vcov(model4, attack$this.geo[attack$successful_attack %in% c(-1, 1)])
coefs4 <- coeftest(model4, vcov_country_4)

#table for models
stargazer(model1, model2, model3, model4, se = list(coefs1[,2], coefs2[,2], coefs3[,2], coefs4[,2]),
          omit = grep('as.factor', names(model1$coefficients)), type = "text")

```